package com.smzd.framework.system.actions;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractScopedController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.business.FunctionService;
import com.smzd.framework.system.business.ModuleService;
import com.smzd.framework.system.business.RoleService;
import com.smzd.framework.system.domain.Function;
import com.smzd.framework.system.domain.Module;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.utils.JsonHelper;
import com.smzd.framework.utils.db.SRPage;

@Controller
@RequestMapping("/system/module/{moduleid}/functions")
@Access(Role.TAG_ROLE_MODULES)
public class FunctionController extends AbstractScopedController<Function, Module> {
    @Autowired
    private FunctionService service;
    @Autowired
    private ModuleService moduleService;

    @Autowired
    private RoleService roleService;

    @Override
    protected Module getScope(int scopeId) {
        return moduleService.getItemById(scopeId);
    }

    @Override
    protected IFGService<Function> getService() {
        return service;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, @PathVariable("moduleid") int scopeId, Function elem,
            BindingResult result) throws Exception {
        return super.toAddPage(model, scopeId, elem, result);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, @PathVariable("moduleid") int scopeId, Function elem,
            BindingResult result) throws Exception {
        return super.postAddData(model, scopeId, elem, result);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, @PathVariable("moduleid") int scopeId, Function elem,
            BindingResult result) throws Exception {
        return super.toListPage(model, scopeId, elem, result);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Access({ Role.TAG_ROLE_MODULES, Role.TAG_ROLE_MENUS })
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model, @PathVariable("moduleid") int scopeId,
            SRPage<Module, Function> page, BindingResult result) throws Exception {
        return super.getListData(model, scopeId, page, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, @PathVariable("moduleid") int scopeId, Function elem,
            BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, scopeId, elem, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, @PathVariable("moduleid") int scopeId, Function elem,
            BindingResult result) throws Exception {
        return super.postEditData(model, scopeId, elem, result);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, @PathVariable("moduleid") int scopeId, Function elem,
            BindingResult result) throws Exception {
        return super.postRemoveData(model, scopeId, elem, result);
    }

    // /////////////扩展功能--角色和功能项的多对多编辑////////////////////
    /**
     * toRoleFunctionsPage：导航到目标角色和功能项的编辑页面。<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param groupId
     * @return
     */
    @RequestMapping(value = "/role-{roleId}", method = RequestMethod.GET)
    @Access(Role.TAG_ROLE_ROLES)
    public String toRoleFunctionsPage(Map<String, Object> model, @PathVariable("moduleid") int scopeId,
            @PathVariable("roleId") int roleId) throws Exception {
        Role role = roleService.getItemById(roleId);
        model.put("item", role);
        return getMyView("rolefunctions.ftl");
    }

    /**
     * getRoleFunctionsData：获取目标角色的功能项配置列表。<br>
     * 目标角色已关联的功能项被排在前面，并且以复选框标志已经关联到当前角色<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param groupId
     * @param page
     * @param result
     * @return
     */
    @RequestMapping(value = "/list/role-{roleId}", method = RequestMethod.POST)
    @ResponseBody
    @Access(Role.TAG_ROLE_ROLES)
    public String getRoleFunctionsData(Map<String, Object> model, @PathVariable("moduleid") int scopeId,
            @PathVariable("roleId") int roleId, SRPage<Module, Function> page, BindingResult result) throws Exception {
        page.setExt(String.valueOf(roleId));
        page.setScope(new Module(scopeId));
        return service.getRoleFunctionListDataJson(page);
    }

    /**
     * saveUserGroups：保存角色和功能项关联的改变情况。<br>
     * 只有新改变的部分数据需要提交服务器端进行处理。<br>
     * 注意：应当限制只有管理员用户可以进行此项操作！
     * 
     * @param model
     * @param groupId
     * @param roleIds
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit/role-{roleId}", method = RequestMethod.POST)
    @ResponseBody
    @Access(Role.TAG_ROLE_ROLES)
    public String saveRoleFunctions(Map<String, Object> model, @PathVariable("moduleid") int scopeId,
            @PathVariable("roleId") int roleId, @RequestParam("funcIds") String funcIds) throws Exception {
        try {
            if (StringUtils.isEmpty(funcIds)) {
                return JsonHelper.getSuccessJsonString("没有任何变动提交！");
            }
            service.saveGroupRoles(roleId, funcIds);
            return JsonHelper.getSuccessJsonString("保存成功！");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

}
